package cn.com.chengmandian.bike.controller;

import cn.com.chengmandian.bike.service.IEbFaultBillInfoService;
import cn.com.chengmandian.bike.service.IEbMessageInfoService;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.com.chengmandian.service.bike.form.MessageAddForm;
import cn.com.chengmandian.service.bike.form.SendWxOrderBillMessageForm;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags = "系统消息相关接口 - 微服务")
@RestController
@RequestMapping("/inner/message")
public class InnerMessageController {
    @Autowired
    private IEbMessageInfoService messageInfoService;
    @Autowired
    private IEbFaultBillInfoService faultBillInfoService;

    @ApiOperation("添加系统消息")
    @PostMapping("/sys/add")
    public AjaxResult addSysMessage(@RequestBody MessageAddForm messageAddForm) {
        Assert.notNull(messageAddForm.getMemberId(), "缺少用户信息");
        Assert.notNull(messageAddForm.getChannel(), "缺少消息频道");
        Assert.isTrue(StrUtil.isNotBlank(messageAddForm.getContent()), "消息内容不能为空");
        Assert.isTrue(messageAddForm.getContent().length() < 400, "消息内容必须400字以内");
        messageInfoService.addSys(messageAddForm.getMemberId(), messageAddForm.getChannel(), messageAddForm.getContent(), messageAddForm.getImage(), messageAddForm.getBusinessType(), messageAddForm.getBusinessId());
        return AjaxResult.success("发送成功");
    }

    @ApiOperation("订单工单消息通知")
    @PostMapping("/wx/order/bill")
    public AjaxResult orderBill(@RequestBody SendWxOrderBillMessageForm form) {
        Assert.notNull(form.getBillId(), "缺少工单ID");

        faultBillInfoService.sendNotice(LocalMapUtil.getAgentInfo(), form.getBillId(), "订单问题工单有新的处理进度，请及时查看", form.getImgUrl());
        return AjaxResult.success();
    }
}
